﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;

namespace Yuanlin.Business
{
    public class ContentHtmlEditControl : YuanlinUserControl,
        IReplyConverter<ContentHtmlReply, GetOperation>,
        IReplyConverter<ContentHtmlCategoryReply, GetListOperation>
    {
        protected ValuedTextBox titleValuedTextBox;
        protected ValuedDropDownList categoryDropDownList;
        protected ValuedEditor descriptionValuedEditor;

        protected IButton SaveButton;
        protected IButton BackButton;
        private ContentHtml contentHtml;
        private EntityList categories;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetContentHtmlRequest(this));
            requestBinders.Add(RequestHelper.BuildGetContentHtmlCategoryListRequest(this));
        }

        protected override void OnLoad(EventArgs e)
        {
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        private void Bind()
        {
            if(contentHtml!=null)
            {
                this.titleValuedTextBox.Value = contentHtml.Title.Value;
                this.descriptionValuedEditor.Value = contentHtml.Body.Value;

                categoryDropDownList.DataSource = categories;
                categoryDropDownList.DataValueField = "EntityId";
                categoryDropDownList.DataTextField = "Title";
                categoryDropDownList.DataBind();
                categoryDropDownList.Value = contentHtml.CategoryId.Value.ToString();
            }   
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect(SiteUrls.Instance().GetContentHtmlListUrl());
        }

        public void SaveButton_Click(object sender, EventArgs e)
        {
            contentHtml.Title.Value =  CensorWordManager.CensorWord(this.titleValuedTextBox.Value);
            contentHtml.Body.Value =  CensorWordManager.CensorWord(this.descriptionValuedEditor.Value);
            contentHtml.CategoryId.Value = int.Parse(categoryDropDownList.Value);

            if (BusinessManager.UpdateContentHtml(contentHtml) == -2)
            {
                Globals.ShowMessage(this.Page, "添加失败", "此信息已经存在！");
                return;
            }

            Page.Response.Redirect(SiteUrls.Instance().GetContentHtmlListUrl());
        }

        #region IReplyConverter<ContentHtmlReply,GetOperation> Members

        public void ConvertReply(ContentHtmlReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                contentHtml = reply.Entity as ContentHtml;
            }
        }

        #endregion

        #region IReplyConverter<ContentHtmlCategoryReply,GetListOperation> Members

        public void ConvertReply(ContentHtmlCategoryReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                categories = reply.EntityList;

                if (!Page.IsPostBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}